
import { _decorator, Component, Node, EventTouch, Sprite, Color } from 'cc';
import { BattleManager } from '../BattleManager';
import { CommandType, Message, MessageType } from '../framework/Message';
import { MessageBase } from '../framework/MessageBase';
import { MessageCenter } from '../framework/MessageCenter';
import { GameData } from '../GameData';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Pet
 * DateTime = Sat Feb 05 2022 15:07:15 GMT+0800 (China Standard Time)
 * Author = vvc_zeng
 * FileBasename = Pet.ts
 * FileBasenameNoExtension = Pet
 * URL = db://assets/script/player/Pet.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('Pet')
export class Pet extends MessageBase {
    static Instance: Pet = null;
    // 所有的宠物信息
    pet;
    // 出战和休息的宠物信息
    // [{ stat: [0, ...0], mid: 10 }, {}]
    battlePet;
    // 上一次死亡的宠物 mid:number
    lastDeadPet: number;
    petComp: Sprite[] = [];
    onLoad() {
        super.onLoad()
        Pet.Instance = this;
        this.node.children.forEach(e => e.on(Node.EventType.TOUCH_END, this.togglePet));
        this.petComp = <Sprite[]>this.node.getComponentsInChildren("cc.Sprite");
    }
    init() {
        this.messageType = MessageType.TYPE_PET;
    }
    initData() {
        this.battlePet = [];
        this.pet = []
        this.lastDeadPet = -1;
    }
    ReveiveMessage(message: Message) {
        super.ReveiveMessage(message);
        if (message.Command == CommandType.COM_PET_INIT) {
            this.initData()
            console.log("初始化宠物栏");
            this.node.active = true;
        }
    }
    // 获得宠物
    getPet(mid) {
        if (this.pet.length > 3) {
            console.log("宠物 已满，不能再获得新宠物")
            return;
        }
        // 出战宠物栏有空位，就放出战宠物里
        if (this.battlePet.length < 2) {
            this.petComp[this.battlePet.length].node.active = true;
            (<Color>this.petComp[this.battlePet.length].color).a = 255;
            // 注意浅拷贝的隐藏坑
            let _np = { stat: Array.from(GameData.Instance.monsterData[mid].b), mid }
            this.pet.push(_np);
            this.battlePet.push(_np);
            // 更新视图
            this.petComp[this.battlePet.length - 1].spriteFrame = GameData.Instance.monsterFrames[mid];
        } else {
            this.petComp[this.pet.length].node.active = true;
            (<Color>this.petComp[this.pet.length].color).a = 100;
            this.pet.push({ stat: Array.from(GameData.Instance.monsterData[mid].b), mid });
            // 更新视图
            this.petComp[this.pet.length - 1].spriteFrame = GameData.Instance.monsterFrames[mid];
        }
        console.log(this.battlePet, this.pet);
    }
    // 捕捉宠物
    catchPet(mid) {
        if (Math.random() < 0.6) {
            // 捕捉成功
            MessageCenter.SendCustomMessage(MessageType.TYPE_MAP, CommandType.COM_MAP_SHOWTIPS, [0, 0, "捕捉成功"]);
            this.getPet(mid);
        } else {
            MessageCenter.SendCustomMessage(MessageType.TYPE_MAP, CommandType.COM_MAP_SHOWTIPS, [0, 0, "捕捉失败"]);
        }
    }
    // 非战斗情况下，切换宠物的出战和休息状态 最多2只出战
    togglePet(e: EventTouch) {
        if (BattleManager.Instance.mid == -1) {
            let i = (<Node>e.target).getSiblingIndex();
            let a = (<Color>this.petComp[i].color).a
            let pet = this.pet[i];
            // 切换宠物的出战和休息状态
            if (a == 100 && this.battlePet.length < 2) {
                this.battlePet.push(pet);
                a = 255
            } else {
                this.battlePet.splice(this.battlePet.map(e => e.mid).indexOf(pet.mid), 1);
                a = 100
            }
            MessageCenter.SendCustomMessage(MessageType.TYPE_MAP, CommandType.COM_MAP_SHOWTIPS, [(<Node>e.target).parent.name, i])
        }
        console.log(this.battlePet, this.pet);
    }
    // 在战斗中失去宠物
    lostPet(pid) {
        this.lastDeadPet = pid;

        let d = this.pet.map(e => e.mid).indexOf(pid);
        this.petComp[d].spriteFrame = null;
        this.petComp[d].node.active = false;
        this.pet.splice(d, 1);
        
        d = this.battlePet.map(e => e.mid).indexOf(pid);
        this.battlePet.splice(d, 1);
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
